package net.plankton.agents.main;

import java.awt.Container;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TestPlayer {

	private JLabel file;
	private JTextArea text;
	private RulesEngine engine;
	private JFileChooser dialog;
	
	public void init() {
		
		engine = new RulesEngine(new TextOutput());
		
		JFrame frame = new JFrame();
		frame.setTitle("Alpbach Testplayer");
		Container c = frame.getContentPane();
		c.setLayout(new GridBagLayout());
		GridBagConstraints g;
		file = new JLabel("Pfad");
		g = new GridBagConstraints();
		g.gridx=0;
		g.gridy=0;
		g.weightx=1.0;
		c.add(file,g);
		JButton load = new JButton("load");
		load.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if (dialog==null) {
					dialog = new JFileChooser();
				}
				dialog.showOpenDialog(file);
				if (dialog.getSelectedFile()!=null) {
					engine.readFile(dialog.getSelectedFile().getAbsolutePath());
					engine.start();
					file.setText(dialog.getSelectedFile().getAbsolutePath());
				}
				
			}
			
		});
		g = new GridBagConstraints();
		g.gridx=1;
		g.gridy=0;
		c.add(load,g);
		
		JButton stop = new JButton("stop");
		stop.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				engine.stop();
				
			}
			
		});
		
		g = new GridBagConstraints();
		g.gridx=2;
		g.gridy=0;
		c.add(stop,g);
		text = new JTextArea(40,100);
		g = new GridBagConstraints();
		g.gridx=0;
		g.gridy=1;
		g.gridwidth=3;
		c.add(new JScrollPane(text),g);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.show();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TestPlayer().init();

	}

	class TextOutput implements SurveillanceOutput {

		public void output(String output) {
			text.setText(output+"\n"+text.getText());
		}
		
	}
	
}
